# -*- coding: utf-8 -*-

import os

from fastapi import FastAPI
from pydantic import BaseModel

from oper_word import save_word
from tools import current_time

app = FastAPI()


class Info(BaseModel):
    campany: str
    legalName: str
    legalPhone: str
    employeeInfor: list


def make_response(**kwargs):
    return dict(**kwargs)


@app.post("/to-word/")
async def doctor(info: Info):
    info = dict(info)

    path = 'words/{}/{}.docx'.format(*current_time().split(' '))
    pdir = os.path.dirname(os.path.abspath(path))
    if not os.path.exists(pdir):
        os.makedirs(pdir)

    success = save_word(path, info)
    if success:
        return make_response(status=0, msg='success')
    else:
        return make_response(status=1, msg='failed')


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=6666)
